﻿using FANUC.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;

namespace FANUC.Controllers
{
    public class CurrentFaultRobotsController : ApiController
    {
        private FANUCDB db = new FANUCDB();


        /// <summary>
        /// GET: api/ApiAlarmHistory/Robot_YH_No
        /// </summary>
        /// <param name="id">Robot_YH_No</param>
        /// <returns>AlarmHistoryDTO</returns>
        [ResponseType(typeof(CurrentFaultRobotsDto))]
        public IHttpActionResult Get()
        {
            #region Test
            if (Helper.IsTest)
            {
                CurrentFaultRobotsDto testData = new CurrentFaultRobotsDto();
                testData.FaultRobots = new List<string>() { "Robot_YH_NO:1", "Robot_YH_NO:2", "Robot_YH_NO:3" };
                return Ok(testData);
            }
            #endregion
            //var alarms = db.Current_Alarm.ToList();
            //if (alarms.Count <= 0)
            //{
            //    CurrentFaultRobotsDto noData = new CurrentFaultRobotsDto();
            //    noData.FaultRobots = new List<string>();
            //    return Ok(noData);
            //}
            //CurrentFaultRobotsDto data = new CurrentFaultRobotsDto();
            //data.FaultRobots = new List<string>();

            //var distinctedAlarms = alarms.GroupBy(a => a.Robot_ID).Select(group => group.First());
            //foreach (var item in distinctedAlarms)
            //{
            //    data.FaultRobots.Add(item.Robot.YH_No);
            //}
            CurrentFaultRobotsDto data = new CurrentFaultRobotsDto();
            data.FaultRobots = new List<string>();
            var notInRunningStateRobots = db.Robot.Where(a => a.Current_State == 2).Select(a => a.YH_No);
            foreach (var item in notInRunningStateRobots)
            {
                data.FaultRobots.Add(item);
            }
            return Ok(data);
        }

        /// <summary>
        /// GET: api/ApiAlarmHistory/Robot_YH_No
        /// </summary>
        /// <param name="id">Robot_YH_No</param>
        /// <returns>AlarmHistoryDTO</returns>
        [ResponseType(typeof(AlarmHistoryDtoOut))]
        public IHttpActionResult Get(string id)
        {
            var alarms = db.Current_Alarm.Where(a => a.Robot.YH_No == id).ToList();
            if (alarms.Count <= 0)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Alarm_History, AlarmRecordDTO>().ForMember(dest => dest.Robot_YH_No, src => src.MapFrom(s => s.Robot.YH_No)));
            List<AlarmRecordDTO> alarmsDTO = AutoMapper.Mapper.Map<List<AlarmRecordDTO>>(alarms);
            AlarmHistoryDtoOut data = new AlarmHistoryDtoOut();
            data.AlarmRecords = alarmsDTO;
            data.Robot_YH_No = id;

            return Ok(data);
        }

        /// <summary>
        /// GET: api/ApiAlarmHistory/?id=Robot_YH_No&pageNumber=2&pageSize=10
        /// </summary>
        /// <param name="id">Robot_YH_No</param>
        /// <param name="pageNumber">Page Number</param>
        /// <param name="pageSize">Page Size</param>
        /// <returns></returns>
        [ResponseType(typeof(AlarmHistoryDtoOut))]
        public IHttpActionResult Get(string id, int pageNumber, int pageSize)
        {
            var alarms = db.Current_Alarm.Where(a => a.Robot.YH_No == id).OrderByDescending(o => o.Time).Skip(pageNumber).Take(pageSize).ToList();
            if (alarms.Count <= 0)
            {
                return NotFound();
            }
            AutoMapper.Mapper.Initialize(cfg => cfg.CreateMap<Alarm_History, AlarmRecordDTO>().ForMember(dest => dest.Robot_YH_No, src => src.MapFrom(s => s.Robot.YH_No)));
            List<AlarmRecordDTO> alarmsDTO = AutoMapper.Mapper.Map<List<AlarmRecordDTO>>(alarms);
            AlarmHistoryDtoOut data = new AlarmHistoryDtoOut();
            data.AlarmRecords = alarmsDTO;
            data.Robot_YH_No = id;

            return Ok(data);
        }

        [ApiExplorerSettings(IgnoreApi = true)]
        public string Options()
        {
            return null;
        }
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
